//
//  VisitorListViewController.m
//  ChatForHot
//
//  Created by FLY on 2018/10/16.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "VisitorListViewController.h"
#import "GuestModel.h"
#import "GuestCell.h"
#import "UserInfoController.h"
#import "PersonalVipViewController.h"
#import "VIPtipView.h"
@interface VisitorListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView * HOTCHATvisitorList;

@property (nonatomic, strong) NSArray *dataArr;

@property (nonatomic, strong) VIPtipView *vipTipView;

@property (nonatomic, strong) UIImageView *HOTCHATlogoImgView;

@property (nonatomic, strong) UILabel *HOTCHATnameLab;

@property (nonatomic, strong) UIView *blankView;

@end

@implementation VisitorListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = NSLocalizedString(@"StrN054", nil);
    
    [self.view addSubview:self.HOTCHATvisitorList];
    [self.HOTCHATvisitorList mas_updateConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self.view);
    }];
    [self HOTCHATgetAllVisitor];
}
//lazzy
- (UITableView *)HOTCHATvisitorList{
    
    if (_HOTCHATvisitorList == nil) {
        
        _HOTCHATvisitorList = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStyleGrouped)];
        _HOTCHATvisitorList.separatorStyle = UITableViewCellSeparatorStyleNone;
        _HOTCHATvisitorList.delegate = self;
        _HOTCHATvisitorList.dataSource = self;
        _HOTCHATvisitorList.backgroundColor = HOTCHATrgb(243, 243, 243);
        [_HOTCHATvisitorList registerClass:[GuestCell class] forCellReuseIdentifier:@"GuestCell"];
        _HOTCHATvisitorList.contentInset = UIEdgeInsetsMake(3, 0, 5, 0);
        [_HOTCHATvisitorList setContentOffset:CGPointMake(0, - 3)];
        
        if (@available(iOS 11.0, *)) {
            _HOTCHATvisitorList.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
    }
    return _HOTCHATvisitorList;
}
- (VIPtipView *)vipTipView{
    if (_vipTipView == nil) {
        _vipTipView = [[VIPtipView alloc] init];
    }
    return _vipTipView;
}
#pragma tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.dataArr.count == 0) {
        
        return 0;
    }else if (self.dataArr.count == 1){
        
        return 1;
    }
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    return self.dataArr.count - 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GuestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GuestCell"];
    if (cell == nil) {
        cell = [[GuestCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"GuestCell"];
    }
    GuestModel *model = [GuestModel new];
    if (indexPath.section == 0) {
        model = [self.dataArr objectAtIndex:indexPath.row];
    }else{
        model = [self.dataArr objectAtIndex:indexPath.row + 1];
    }
    cell.model = model;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    GuestModel *model = [GuestModel new];
    if (indexPath.section == 0) {
        model = self.dataArr[indexPath.row];
    }else{
        if (HOTCHATAPPINFO.isVIP.integerValue != 1) {
            [self HOTCHATgoVipVC];
            return;
        }
        model = self.dataArr[indexPath.row+1];
    }
    [self HOTCHATgoUserInfoViewController:model];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 50;
    }
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        
        return self.vipTipView;
    }
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (void)HOTCHATgoUserInfoViewController:(GuestModel *)model{
    
    UserInfoController *userVC = [[UserInfoController alloc]init];
    userVC.accountId = model.accountid;
    userVC.IMID = model.IMUser;
    [self.navigationController pushViewController:userVC animated:YES];
}
- (void)HOTCHATgoVipVC{
    
    PersonalVipViewController *vipVC = [[PersonalVipViewController alloc]init];
    [self.navigationController pushViewController:vipVC animated:YES];
}
//拿取数据
- (void)HOTCHATgetAllVisitor{
    
    self.dataArr = [GuestModel HOTCHATgetAllList];
    
    [GuestModel HOTCHATremarkAllissee];
    
    if (self.dataArr.count == 0) {
        
        [self HOTCHATlayoutyBlankView];
    }
}
- (void)HOTCHATlayoutyBlankView{
    
    [self.view addSubview:self.HOTCHATlogoImgView];
    [self.view addSubview:self.HOTCHATnameLab];
    [self.HOTCHATlogoImgView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).offset(100);
        make.centerX.equalTo(self.view);
    }];
    [self.HOTCHATnameLab mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.HOTCHATlogoImgView.mas_bottom).mas_offset(15);
        make.centerX.equalTo(self.view);
        
    }];
}

- (UIImageView *)HOTCHATlogoImgView{
    if (_HOTCHATlogoImgView == nil) {
        
        _HOTCHATlogoImgView = [[UIImageView alloc] init];
        _HOTCHATlogoImgView.image = [UIImage imageNamed:@"m_ic_no_visitor"];
        
    }
    return _HOTCHATlogoImgView;
}
- (UILabel *)HOTCHATnameLab{
    if (_HOTCHATnameLab == nil) {
        _HOTCHATnameLab = [[UILabel alloc] init];
        _HOTCHATnameLab.font = [UIFont systemFontOfSize:15];
        _HOTCHATnameLab.textColor = HOTCHATHexColor(0xAAAAAA);
        _HOTCHATnameLab.textAlignment = NSTextAlignmentCenter;
        _HOTCHATnameLab.text = NSLocalizedString(@"StrN055", nil);
    }
    return _HOTCHATnameLab;
}
@end
